// 存储缓存相关代码
import Cookies from 'js-cookie'
import { randomChar, getTodayStr } from './tools.js'
import { freeReqNum } from './config.js'
// import { useRoute } from 'vue-route'

// 限制次数
// cookie24小时，并不是凌晨12点过期，如果在过期前又访问了网站，cookie又会被重置，导致cookie永远无法过期。因此解决办法是在cookie的name上加上日期时间戳。过了12点，之前的cookie就丢弃不用

const numsLimitKey = '_nums'
const phoneLimitLkey = '_cphone'
const nameLimitLkey = '_aname'
const nickNameLimitLkey = '_nnickname'



function getDownloadTimeValue(str) {
  if (str) {
    return parseInt(str) || 0;
  }
  return 0;
}
// 获取此时address请求的次数
function getCurReqNum() {
  // console.log('getCurReqNum')
  const route = useRoute()
  // console.log('route:', route)
  let routeName = route.name
  // let cookieName = routeName + '/' + numsLimitKey + '/' + getTodayStr()
  let cookieName = routeName + '/' + numsLimitKey

  let valueStr = Cookies.get(cookieName) || window.sessionStorage.getItem(cookieName) || window.localStorage.getItem(cookieName)
  let reqNum = getDownloadTimeValue(valueStr);
  return reqNum
}

function setCurResNum(value = 0) {
  const route = useRoute()
  // console.log('route:', route)
  let routeName = route.name
  // let cookieName = routeName + '/' + numsLimitKey + '/' + getTodayStr()
  let cookieName = routeName + '/' + numsLimitKey
  let tValue = value + randomChar();
  Cookies.set(cookieName, tValue, { expires: 7 });
  window.sessionStorage.setItem(cookieName, tValue);
  window.localStorage.setItem(cookieName, tValue);
}
function getTodayReqNum() {
  // console.log('getCurReqNum')
  const route = useRoute()
  // console.log('route:', route)
  let cookieName = getTodayStr()

  let valueStr = Cookies.get(cookieName) || window.sessionStorage.getItem(cookieName)
  let reqNum = getDownloadTimeValue(valueStr);
  return reqNum
}

function setTodayResNum(value = 0) {
  const route = useRoute()
  // console.log('route:', route)
  let cookieName = getTodayStr
  let tValue = value + randomChar();
  Cookies.set(cookieName, tValue, { expires: 1 });
  window.sessionStorage.setItem(cookieName, tValue);
}

function removeCurAddressResNum() {
  const route = useRoute()
  // console.log('route:', route)
  let routeName = route.name
  let cookieName = routeName + '/' + numsLimitKey
  Cookies.remove(cookieName)
  window.sessionStorage.removeItem(cookieName)
}

// 手机号相关限制

function getCurPhoneReqNum() {
  let valueStr = Cookies.get(phoneLimitLkey) || window.sessionStorage.getItem(phoneLimitLkey)
  let reqNum = getDownloadTimeValue(valueStr);
  return reqNum
}

function setCurPhoneResNum(value = 0) {
  let tValue = value + randomChar();
  Cookies.set(phoneLimitLkey, tValue, { expires: 1 });
  window.sessionStorage.setItem(phoneLimitLkey, tValue);
}


// 姓名相关限制

function getCurNameReqNum() {
  let valueStr = Cookies.get(nameLimitLkey) || window.sessionStorage.getItem(nameLimitLkey)
  let reqNum = getDownloadTimeValue(valueStr);
  return reqNum
}

function setCurNameResNum(value = 0) {
  let tValue = value + randomChar();
  Cookies.set(nameLimitLkey, tValue, { expires: 1 });
  window.sessionStorage.setItem(nameLimitLkey, tValue);
}

// 昵称相关限制

function getCurNickNameReqNum() {
  let valueStr = Cookies.get(nickNameLimitLkey) || window.sessionStorage.getItem(nickNameLimitLkey)
  let reqNum = getDownloadTimeValue(valueStr);
  return reqNum
}

function setCurNickNameResNum(value = 0) {
  let tValue = value + randomChar();
  Cookies.set(nickNameLimitLkey, tValue, { expires: 1 });
  window.sessionStorage.setItem(nickNameLimitLkey, tValue);
}

// 判断是否可以请求address的api
export function canGetDataFromServer() {
  let times = getCurReqNum()
  // console.log('times:', times)
  setCurResNum(times + 1)


  return times < freeReqNum
}

// export function canGetPhone () {
//   let times = getCurPhoneReqNum()
//   setCurPhoneResNum(times + 1)
//   return times < freeReqNum
// }

// export function canGetName () {
//   let times = getCurNameReqNum()
//   setCurNameResNum(times + 1)
//   return times < freeReqNum
// }

// export function canGetNickName () {
//   let times = getCurNickNameReqNum()
//   console.log('times:', times)
//   setCurNickNameResNum(times + 1)
//   return times < freeReqNum
// }

